﻿@using Microsoft.AspNetCore.Components
@inject NavigationManager navigationManager
@implements IDisposable
@inject AuthenticationStateProvider authStateProvider

<ul class="breadcrumb">
    <li>
        <a href="/">@RootLinkTitle</a>
    </li>
    @foreach (var path in paths.Select((value, i) => (value, i)))
    {
        if (path.i == paths.Count())
        {
            <li><a href="@(baseUrl + String.Join("/", paths))">@path.value.Replace("_", " ")</a></li>
        }
        else
        {
            <li>@path.value.Replace("_", " ")</li>
        }
    }
</ul>

@code {
    // This is just for demo and is VERY Hackish  for several reasons
    // Blazor does not have page implemented for client side... as far as I know so we split page names with underscore for now.
    // Path system is hack as well as the Url.. Maybe you have a better solution?

    public bool IsLoggedIn = false;

    [Parameter]
    public string RootLinkTitle { get; set; }

    public string[] paths;
    public string baseUrl;

    protected override async Task OnParametersSetAsync()
    {
    }

    protected override async Task OnInitializedAsync()
    {
        baseUrl = navigationManager.BaseUri;
        await BuildBreadcrumbsAsync();
    }
    void OnLocationChanges(object sender, LocationChangedEventArgs e) => BuildBreadcrumbsAsync();

    private async Task BuildBreadcrumbsAsync()
    {
        string uri = navigationManager.Uri.Replace(baseUrl, "").Trim();

        paths = String.IsNullOrEmpty(uri) ? new string[] { } : uri.Split('/');
        StateHasChanged();
    }

    public void Dispose()
    {
    }
}